----#include "data\functions\Systems\Miscs\ChuanSongCommonFunc.lua" once

NpcDialog[100] = {}
local t = NpcDialog[100]

t.main = function(sysarg)
	local nCount = table.getn(ChuanSongYuanCfg.mapTitle)
	local pack = DataPack.allocPacket(sysarg, LogicCmd.npcDialogSys.sysID, LogicCmd.npcDialogSys.sub.sCSYDlg)
	if not pack then 
		return 
	end
	
	DataPack.writeUint64(pack, Actor.getNpc(sysarg))
	DataPack.writeByte(pack, nCount)			-- 版块数量
	for i=1, nCount do
		if i < nCount then
			local dataCfg = ChuanSongYuanCfg.mapdata[i]
			local btnCount = #dataCfg
			DataPack.writeString(pack, ChuanSongYuanCfg.mapTitle[i])	-- 标题
			DataPack.writeByte(pack, btnCount)			-- 按钮数量
			for j=1, btnCount do
				DataPack.writeInt(pack, dataCfg[j][1])		-- 场景id
				DataPack.writeString(pack, dataCfg[j][5])	-- 按钮名字
				DataPack.writeString(pack, string.format("mapto,%d,%d", i, j))	-- 回调函数名 + 参数
				DataPack.writeWord(pack, dataCfg[j][3])	-- 等级
				DataPack.writeByte(pack, dataCfg[j][4])	-- 转生数
			end
		elseif i == nCount then 		--最后一个要特殊处理（就是区域4）
			local guildId = Actor.getGuildId( sysarg )
			local guildOwnScenes = GetGuildOwnSceneByGuildId( guildId ) or {}
			local chuanSongScenes= GetChuanSongScenesOfGuildOwn( guildOwnScenes )
			local btnCount = #chuanSongScenes
			DataPack.writeString(pack, ChuanSongYuanCfg.mapTitle[i])	-- 标题
			DataPack.writeByte(pack, btnCount);			-- 按钮数量
			for j=1, btnCount do
				local guildSceneId = chuanSongScenes[j]
				local sceneId, buttonName, funcName, level, circle = GetChuanSongDataBySceneId( guildSceneId )
				if sceneId > 0 and buttonName then
					DataPack.writeInt(pack, sceneId)			-- 场景id
					DataPack.writeString(pack, buttonName)		-- 按钮名字
					DataPack.writeString(pack, funcName)		-- 回调函数名 + 参数
					DataPack.writeWord(pack, level)				-- 等级
					DataPack.writeByte(pack, circle)			-- 转生数
				end
			end	
		end
	end
	DataPack.flush(pack)
	return ""
end

function GetChuanSongDataBySceneId( guildSceneId )
	local areaIdx = 4
	local dataCfg = ChuanSongYuanCfg.mapdata[areaIdx]			--只找4-"占领区"
	local btnCount = #dataCfg
	for btnIdx=1, btnCount do
		local sceneId = dataCfg[btnIdx][1]
		if guildSceneId > 0 and guildSceneId == sceneId then
			local buttonName 	= dataCfg[btnIdx][5]
			local funcName		= string.format("mapto,%d,%d", areaIdx, btnIdx)
			local level 		= dataCfg[btnIdx][3]
			local circle 		= dataCfg[btnIdx][4]
			return sceneId, buttonName, funcName, level, circle
		end
	end
	--print("GetChuanSongDataBySceneId, no guildSceneId="..guildSceneId)
	return 0, nil, nil, nil, nil
end

--[[
获取传送配置的场景（按传送配置排序）
]]
function GetChuanSongScenesOfGuildOwn( guildOwnScenes )
	local areaIdx = 4
	local chuansongScenes = {}
	local dataCfg 	= ChuanSongYuanCfg.mapdata[areaIdx]		--只找4-"占领区"
	local btnCount 	= #dataCfg
	for btnIdx=1, btnCount do
		local sceneId = dataCfg[btnIdx][1]
		for i,guildSceneId in ipairs( guildOwnScenes ) do
			if guildSceneId > 0 and guildSceneId == sceneId then		--只保留传送配中的
				table.insert( chuansongScenes, guildSceneId )
			end
		end
	end

	return chuansongScenes
end

------------------------------------------
--这个函数需要修改 后端只能接受前端的索引 其他判断条件全部后端判断 hwei
-- mapto	传送功能
-- areaIdx		区域索引
-- btnIdx			按钮索引
t.mapto = function(sysarg, areaIdx, btnIdx)
	moveToScene(sysarg, tonumber(areaIdx), tonumber(btnIdx))
end
